package com.components.form
{
	
	/**
	 * @auth Chunning
	 * @purpose DATA FORMAT of item in registerform
	 * @date 2009-4-2
	 * */
	 
	import flash.events.FocusEvent;
	import flash.text.TextField;
	
	public class FormItem
	{
		//START
		
		//PROPERTY
		
		/**
		 * checkMethods item data format:
		 * property:
		 * correct:Boolean
		 * check:Function
		 * name:String
		 * */
				
		private var checkMethods:Array = new Array;
				
		private var checkIndex:uint;
		
		private var _unit:FormItem;
		
		public var item:TextField;
		
		public var name:String;
		
		public var form:FormController;
		
		public var isLoading:Boolean;
		
		public var defaultContent:String;
				
		private var oldContent:String;
		
		public var isPassword:Boolean;
		
		//CONSTRUCTOR
		public function FormItem(item:TextField,form:FormController,name:String)
		{
			
			this.form = form;
			
			this.item = item;
			
			this.defaultContent = this.item.text == null ? "" : this.item.text;
			
			this.name = name;
			
			if(this.name.indexOf("password") > -1 || this.name.indexOf("pwd") > -1)
			{
				
				this.item.addEventListener(FocusEvent.FOCUS_IN,this.passwordFocusIn);
				
			}
						
		}
		
		//METHODS
				
		//set check methods...
		public function addCheckMethods(arr:Array):void
		{
			
			for(var i:uint;i<arr.length;i++)
			{
				
				this.addCheckMethod(arr[i].check,arr[i].name);
				
			}
			
		}
		
		//focus in handler...
		private function passwordFocusIn(evt:FocusEvent):void
		{
			
			this.item.displayAsPassword = true;
			
			this.isPassword = true;
			
		}
		
		//add check function...
		public function addCheckMethod(checkFoo:Function,name:String = null):void
		{
			
			this.checkMethods.push({correct:false,check:checkFoo,name:name});
												
		}
		
		//check...
		public function check():void
		{
							
			if(this.checkMethods.hasOwnProperty(this.checkIndex))
			{
				
				this.checkMethods[this.checkIndex].check(this);
								
			}
											
		}
		
		//return check...
		public function returnCheckResult(checkname:String,correct:Boolean):void
		{
			if(!correct)
			{
				
				return;
				
			}		
								
			for(var i:uint = 0;i < this.checkMethods.length;i++)
			{
				
				if(this.checkMethods[i].name == checkname && i == this.checkIndex)
				{
					this.checkMethods[i]["correct"] = correct;
									
					this.checkIndex++;
					
					this.check();
					
					return;					
				}
				
			}
					
		}		
		
		//is corrected ?
		public function get isCorrect():Boolean
		{
						
			for(var i:uint;i < this.checkMethods.length;i++)
			{
				
				if(!this.checkMethods[i]["correct"])
				{
																
					return false;
					
				}	
				
			}
			
			return true;
			
		}
		
		//set is corrent...
		public function set isCorrect(val:Boolean):void
		{
			
			for(var i:uint;i < this.checkMethods.length;i++)
			{
				
				this.checkMethods[i]["correct"] = val;
								
			}
						
		}
		
		//remove check by name...
		public function removeCheckByName(name:String):void
		{
			
			for(var i:uint;i < this.checkMethods.length;i++)
			{
								
				if(this.checkMethods[i].name == name)
				{
					
					this.checkMethods.splice(i,1);					
					
					break;
					
				}
				
			}
			
			
		}
		
		//reset ...
		public function reset():void
		{
			
			if(!this.changed && this.isCorrect)
			{
				
				return;
				
			}
						
			this.checkIndex = 0;
			
			for(var i:uint;i < this.checkMethods.length;i++)
			{
				
				this.checkMethods[i]["correct"] = false;
				
			}
			
			this.check();
			
		}
		
		//get info blank...
		public function get infoBlank():TextField
		{
			
			return this.form.getInfoByName(this.name);
			
		}
		
		//set unit check item...
		public function set unitCheckItem(val:FormItem):void
		{
			
			this._unit = val;
			
			this._unit._unit = this;
			
			
		}
		
		//get unit check item...
		public function get unitCheckItem():FormItem
		{
			
			return this._unit;
			
		}
		
		//get need check...
		public function get changed():Boolean
		{
			
			var _need:Boolean;
			
			_need = this.item.text != this.oldContent;
			
			this.oldContent = this.item.text;
							
			return _need;
			
		}
		
		
		//END
	}
}